/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
 package com.wealth.order.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

import com.wealth.order.entity.OrderItem;
import com.wealth.order.repository.OrderItemDao;
import com.wealth.order.service.IOrderItemService;

import iaas.persistence.DynamicSpecifications;
import iaas.persistence.SearchFilter;
import iaas.persistence.SearchFilter.Operator;
import iaas.service.impl.BaseServiceImpl;
import iaas.utils.CommonUtil;

/**
 * @author chan
 * @version 1.0
 * @since 1.0
 */
 
@Component
public class OrderItemServiceImpl extends BaseServiceImpl<OrderItem> implements IOrderItemService{

	@Autowired
	private OrderItemDao orderItemDao;

	/** 查询订单明细 */
	@Override
	public List<OrderItem> findByOrderId(Long orderId) {
		return orderItemDao.findByOrderId(orderId);
	}
	
	/**
	 * 导出查询出的所有数据
	 * @return
	 */
	public HSSFWorkbook exportFile(Long corpid,Long merchantid,Integer paystatus,String startTime,String endTime){
		try { 
			//创建wb
			HSSFWorkbook wb = new HSSFWorkbook();    
	        HSSFSheet sheet = wb.createSheet("Order");  
	        
	        Row row = sheet.createRow(0);
	        row.createCell(0).setCellValue("订单编号");    
            row.createCell(1).setCellValue("订购日期");    
            row.createCell(2).setCellValue("手机号");    
            row.createCell(3).setCellValue("昵称");    
            row.createCell(4).setCellValue("收货人");    
            row.createCell(5).setCellValue("联系电话");    
            row.createCell(6).setCellValue("收货地址");    
            row.createCell(7).setCellValue("商品编号");    
            row.createCell(8).setCellValue("商品名称");    
            row.createCell(9).setCellValue("商品单价");    
            row.createCell(10).setCellValue("商品数量");    
            row.createCell(11).setCellValue("总金额");   
            
	        //step2 遍历openid
	        Collection<SearchFilter> filters = new ArrayList<SearchFilter>();
	        filters.add(new SearchFilter("order.paystatus", Operator.EQ, paystatus));
	        if(merchantid!=null){
	        	filters.add(new SearchFilter("order.merchant.id", Operator.EQ, merchantid));
	        }
	        filters.add(new SearchFilter("order.createTime", Operator.GE, startTime));
	        filters.add(new SearchFilter("order.createTime", Operator.LE, endTime));
	        Specification<OrderItem> spec = DynamicSpecifications.bySearchFilter(filters, OrderItem.class);
	        List<OrderItem> dblist=orderItemDao.findAll(spec);
	        for (int i = 0; i < dblist.size(); i++) {
	        	OrderItem item = dblist.get(i);
	            row = sheet.createRow(i+1);    
	            row.createCell(0).setCellValue("`"+item.getOrderno());    
	            row.createCell(1).setCellValue(CommonUtil.formatDate(item.getCreateTime()));
	            row.createCell(2).setCellValue(item.getUsername());   
	            row.createCell(3).setCellValue(item.getNickname());    
	            row.createCell(4).setCellValue(item.getReceiver()!=null?item.getReceiver():"");    
	            row.createCell(5).setCellValue(item.getTelephone()!=null?item.getTelephone():"");    
	            row.createCell(6).setCellValue(item.getAddress()!=null?item.getAddress():"");    
	            row.createCell(7).setCellValue(item.getGoods().getId());    
	            row.createCell(8).setCellValue(item.getGoods().getName());    
	            row.createCell(9).setCellValue(item.getPrice());    
	            row.createCell(10).setCellValue(item.getAmount());    
	            row.createCell(11).setCellValue(item.getPrice()*item.getAmount());    
	        }
	        wb.close();
	        return wb;
	    }catch (Exception e){  
            e.printStackTrace(); 
            return null;
        }  
	}
	
}
